<template>
  <div class="default-column">
    <div class="edit-column">
      <el-button type="primary" icon="el-icon-plus" circle @click="addTaskRule" size="small"></el-button>
    </div>

    <el-table v-loading="ruleListLoading" element-loading-text="拼命加载中"
              :data="ruleList" border stripe class="item-table" size="mini">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="center" prop="serviceList" label="客服ID列表">
        <template slot-scope="scope">
          <span v-for="item in scope.row.serviceList" style="margin: 5px; color: #409EFF">{{item.name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="taskCodeList" label="任务类型列表">
        <template slot-scope="scope">
          <p v-for="type in scope.row.taskTypeList" style="color: firebrick; width: 100%">{{type.taskName}}</p>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="userType" label="用户类型" width="100px"></el-table-column>
      <el-table-column align="center" prop="cron" label="cron时间"></el-table-column>
      <el-table-column align="center" prop="allotment" label="分配个数" width="100px"></el-table-column>
      <el-table-column align="center" prop="memo" label="备注" ></el-table-column>
      <el-table-column align="center" prop="status" label="状态" width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="mini" type="success">运行</el-tag>
          <el-tag v-else-if="scope.row.status === 0" size="mini" type="info">暂停</el-tag>
          <el-tag v-else size="mini" type="warning">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 0" type="text" size="mini" style="color: #409EFF" @click="changeTaskRuleStatus(scope.row)">运行</el-button>
          <el-button v-else type="text" size="mini" style="color: #909399" @click="changeTaskRuleStatus(scope.row)">暂停</el-button>

          <el-button type="text" size="mini" style="color: #1E90FF" @click="clickUpdateTaskRule(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" style="color: red" @click="deleteTaskRule(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="page.current"
        :page-size="page.size"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>

    <rule-dialog></rule-dialog>

  </div>
</template>

<script>
  import RuleDialog from "../../components/task/RuleDialog.vue";

  export default {
    components: {RuleDialog},
    name: 'Rule',
    data () {
      return {
        ruleListLoading: false,
        ruleList: [],
        page: {
          current: 1,
          size: 10,
          total: 0
        },
      }
    },
    created() {
      this.loadRuleList();
    },
    methods: {
      handleSizeChange(val) {
        this.page.current = 1;
        this.page.size = val;
        this.loadRuleList();
      },
      handleCurrentChange() {
        this.loadRuleList();
      },

      // 新增/更新任务
      addTaskRule() {
        this.$store.state.ruleInfo = {};
        this.loadPersonList();
        this.loadTaskTypeList();
        this.$store.state.ruleDialogTitle = this.$store.state.addRuleDialogTitle;
        this.$store.state.ruleDialogVisible = true;
      },
      clickUpdateTaskRule(taskRule) {
        this.loadPersonList();
        this.loadTaskTypeList();
        this.getCronText(taskRule.cron);

        this.$store.state.ruleInfo.id = taskRule.id;
        this.$store.state.ruleInfo.status = taskRule.status;
        this.$store.state.ruleInfo.ids = [];
        for (let i = 0; i < taskRule.serviceList.length; i++) {
          this.$store.state.ruleInfo.ids.push(taskRule.serviceList[i].id);
        }
        this.$store.state.ruleInfo.types = [];
        for (let i = 0; i < taskRule.taskTypeList.length; i++) {
          this.$store.state.ruleInfo.types.push(taskRule.taskTypeList[i].taskCode);
        }
        this.$store.state.ruleInfo.cron = taskRule.cron;
        this.$store.state.ruleInfo.memo = taskRule.memo;
        this.$store.state.ruleInfo.allotment = taskRule.allotment;

        this.$store.state.ruleDialogTitle = this.$store.state.updateRuleDialogTitle;
        this.$store.state.ruleDialogVisible = true;
      },
      deleteTaskRule(taskRule) {
        this.$confirm('此操作将删除该分配规则, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteTaskRuleInternal(taskRule);
        }).catch(e => {})
      },
      async deleteTaskRuleInternal(taskRule) {
        let data = await this.$$put('/rule/delete', {
          id: taskRule.id,
          allotServiceId:  taskRule.serviceIdList,
          allotTaskCode:   taskRule.taskCodeList,
          userType:   taskRule.userType,
          autoAllotTime: taskRule.cron,
          allotment: taskRule.allotment,
          memo:   taskRule.memo,
        });
        if (this.checkResponse(data)) {
          this.$message.success('删除成功!');
          this.loadRuleList();
        }
      },
      async changeTaskRuleStatus(taskRule) {
        if (taskRule.status !== 0 && taskRule.status !== 1) {
          return;
        }
        taskRule.status = taskRule.status === 1 ? 0 : 1;

        let data = await this.$$put('/rule/status/' + taskRule.status + '/' + taskRule.id);
        if (this.checkResponse(data)) {
          this.$message.success("更新任务状态成功");
          this.loadRuleList();
        }
      },

      async loadRuleList() {
        this.ruleListLoading = true;
        let data = await this.$$post('/rule/page/list', {
          type: 0,
          current: this.page.current,
          size: this.page.size
        });
        this.ruleListLoading = false;
        if (this.checkResponse(data)) {
          data = data.data.data;
          this.page.total = data.total;
          this.ruleList = data.data;
        }
      },
      // 添加规则时加载客服人员列表
      loadPersonList() {
        this.$store.state.personList = [];
        this.$$get('/customer/list').then(res => {
          if (this.checkResponse(res)) {
            res.data.data.forEach(person => {
              this.$store.state.personList.push({
                key: person.id,
                label: person.name,
                disabled: false
              })
            })
          }
        })
      },
      // 添加规则时加载任务类型列表
      loadTaskTypeList() {
        this.$store.state.taskTypeList = [];
        this.$$get('/type/list').then(res => {
          if (this.checkResponse(res)) {
            res.data.data.forEach(taskType => {
              this.$store.state.taskTypeList.push({
                key: taskType.taskCode,
                label: taskType.taskName,
                disabled: false
              })
            })
          }
        }).catch(e => {this.$message.error('异常 ' + e)})
      },
      getCronText(cron) {
        this.$$put("/rule/check/cron", { cron: cron }).then(res => {
          this.$store.state.ruleInfo.cronText = res.data.data;
        }).catch(e => {this.$message.error('异常 ' + e)})
      },
    }
  }
</script>

<style lang="less" scoped>
</style>
